{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import urllib\n",
    "import datetime\n",
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def unicode(string):\n",
    "    new_str = ''\n",
    "    for ch in string:\n",
    "        if '\\u4e00' <= ch <= '\\u9fff':\n",
    "            new_str += hex(ord(ch)).replace('0x', '\\\\u')\n",
    "        else:\n",
    "            new_str += ch\n",
    "    return new_str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getHtml(url):\n",
    "    header = {\n",
    "        \"User-Agent\": \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36\",\n",
    "        \"Host\": \"index.baidu.com\",\n",
    "        \"Referer\": \"http://index.baidu.com/v2/main/index.html\",\n",
    "        \"Cipher-Text\": \"1669532696663_1669533052366_RyyMM+fdGvWortq9YbJ0koVK4a9oSDWIjSn3HfVGWBjf8YNrsNta+slyYdOGbfG8HQ+JMIjHq/3nyE3jmJeOS2Vtd+oCCz+rNmov/glakdwMprFDveTaJ++iR77w7tQxLMCNZJl0yfHiman8Amaxv7ySQ/wJ6Phr9r+72+MoDWTItVETxGdWyKrPu5A1OgxdjaWBQAC8HBQkeXfHWd7CMqL2fm+RYLCeKscbMeWWmeDGpkIeOiTNQUXzlRBjBcBW/29BHC8MTe9vq1rQCQZJSa44DGB2JmBRC+NskyoxTtVcND8IaUA9hsXMzzBPCq9YQY7E1/jBQW54IMAJ+QvI0Md1PcCLG2DclQ+M5rcwrTxth0cJwfEZfpk/isunTBiE\",\n",
    "        'Cookie':  'BIDUPSID=7175F15814438860CAFB14CF883E7713; PSTM=1651827154; MCITY=-%3A; BDUSS=F1SnppUURraUcwSnI2ZU5TM3dvOWNNeFdUcFFRRXg3VXF4ZFV3Y0Z1MUxDV2xqSVFBQUFBJCQAAAAAAQAAAAEAAADBav8mwrmx39Xf4q8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEt8QWNLfEFjb1; BAIDUID=70BB0450DEC3F9BA96830FB49D5B6777:FG=1; Hm_up_d101ea4d2a5c67dab98251f0b5de24dc=%7B%22uid_%22%3A%7B%22value%22%3A%224949240513%22%2C%22scope%22%3A1%7D%7D; BDORZ=FFFB88E999055A3F8A630C64834BD6D0; BAIDUID_BFESS=70BB0450DEC3F9BA96830FB49D5B6777:FG=1; BA_HECTOR=2524a524ak8k0h0g0121a5cm1ho5v3g1e; ZFY=v3CS9rw2uLRxuUdY6t3NXUTv0J87jhdTUEtHTXbfIUc:C; BDRCVFR[UvtsIQXhuz3]=u2eaX9cFRPtUyqdUANVQhPEUf; delPer=0; PSINO=5; H_PS_PSSID=; Hm_lvt_d101ea4d2a5c67dab98251f0b5de24dc=1669413002,1669449833,1669464346,1669533038; bdindexid=rn22kh6ik4834a5mud2kkd9481; SIGNIN_UC=70a2711cf1d3d9b1a82d2f87d633bd8a04195454899Uh3TkeDJmlEMynO%2F7oyZB7okMbSDDwm90WkGn33VKeb2BJ5xrkidoGHak5mT69JJ1g47wvvqiaJv8H5ezO1BIC1UOSq8KpEdUZx7lVJg0jJg0WEmlssxuG%2Fc7xyW6dwDjN4TUX0uiznU02yfQT5yht8Sn7IcEkmtu9BLeaBW%2FZaWx85DOAQWaOsKFQ88D3lvYpdIl6SwhxmV%2Bo7WsewzU4vWgSyRBQtQjkAtzvn9HLlcFfquFCCRhklx6Gruh6LrRK7ESrlKarpBCcnz%2BlqGP62NmiEv%2Bkn%2FLAAC2oUhmaojp9UgTIxlfYREtEvPWt5s23163835072258574126855669766820; __cas__rn__=419545489; __cas__st__212=3f2d45e5a05b7ff2e1ebc147ecb8037fcb7e7ce4ceeed7f2d631810a3ece736e35f208973225067c37441ce1; __cas__id__212=43903776; CPID_212=43903776; CPTK_212=1924215375; RT=\"z=1&dm=baidu.com&si=f05fe424-702a-497d-a731-e6b1c321e8d9&ss=laz0v4oe&sl=4&tt=3j1&bcn=https%3A%2F%2Ffclog.baidu.com%2Flog%2Fweirwood%3Ftype%3Dperf\"; Hm_lpvt_d101ea4d2a5c67dab98251f0b5de24dc=1669533052; ab_sr=1.0.1_ODE1NzI2Y2IyNjAyZWQ0ZDRiYzFmMTkzMTE3M2FiZDc3Y2ExZDNmMzA1MWJiZWQzYThkZGJiYTdmODViMzQyNTZlYjMxOTY0MTVhMmRjMzdmNzJlYmFiZDFmOWRlMTUxMDA3YTkwYWNhY2ZjZjliOTIzNTQ3ODk4ZDc4OGU3MjE4OTJmNTk0MmM3NGMxZmUyNmNmZTY1ZTVjOGU1ZjhlYw==; BDUSS_BFESS=F1SnppUURraUcwSnI2ZU5TM3dvOWNNeFdUcFFRRXg3VXF4ZFV3Y0Z1MUxDV2xqSVFBQUFBJCQAAAAAAQAAAAEAAADBav8mwrmx39Xf4q8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEt8QWNLfEFjb1'\n",
    "    }\n",
    "    url_requests = urllib.request.Request(url = url, headers = header)\n",
    "    response = urllib.request.urlopen(url_requests)\n",
    "    content = response.read()\n",
    "    return content.decode()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def decrypt(t, e):\n",
    "    n = list(t)\n",
    "    i = list(e)\n",
    "    a = {}\n",
    "    result = []\n",
    "    ln = int(len(n) / 2)\n",
    "    start = n[ln:]\n",
    "    end = n[:ln]\n",
    "    for j, k in zip(start, end):\n",
    "        a.update({k: j})\n",
    "    for j in e:\n",
    "        result.append(a.get(j))\n",
    "    return ''.join(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_ptbk(uniqid):\n",
    "    url = 'http://index.baidu.com/Interface/ptbk?uniqid={}'\n",
    "    resp = getHtml(url.format(uniqid))\n",
    "    re_str = '\"data\":\"(.*?)\"'\n",
    "    return re.findall(re_str, resp)[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getData(word, start_time, end_time):\n",
    "    url = \"https://index.baidu.com/api/SearchApi/index?area=917&word=[[%7B%22name%22:%22{}%22,%22wordType%22:1%7D]]&startDate={}&endDate={}\".format(word, start_time, end_time)\n",
    "    data = getHtml(url)\n",
    "    uniqid = re.findall('\"uniqid\":\"(.*?)\"', data)[0]\n",
    "    data = re.findall('\"all\":.*?\"data\":\"(.*?)\"', data)[0]\n",
    "    ptbk = get_ptbk(uniqid)\n",
    "    result = decrypt(ptbk, data)\n",
    "    result = result.split(',')\n",
    "    start = start_time.split(\"-\")\n",
    "    end = end_time.split(\"-\")\n",
    "    a = datetime.date(int(start[0]), int(start[1]), int(start[2]))\n",
    "    b = datetime.date(int(end[0]), int(end[1]), int(end[2]))\n",
    "    node = 0\n",
    "    result_str = ''\n",
    "    for i in range(a.toordinal(), b.toordinal()):\n",
    "        date = datetime.date.fromordinal(i)\n",
    "        result_str += str(date) + ',' + '{}'.format(result[node]) + '\\n'\n",
    "        node += 1\n",
    "    f = open('{}.csv'.format(word.encode().decode('unicode_escape')), 'w')\n",
    "    f.write(result_str)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "word = ['发热', '干咳', '乏力']\n",
    "unicode_list = []\n",
    "for i in range(len(word)):\n",
    "    unicode_list.append(unicode(word[i]))\n",
    "area = 917\n",
    "start_time = '2021-01-01'\n",
    "end_time = '2022-01-01'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(getData(unicode_list[0], start_time, end_time))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.9.10 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "ab1d930aa3d38ca1f2d3995a6e83c91a45283867d558d974f0694a0f6727de6f"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
